using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Alogient.Cameleon.Forms.Sdk;
using Alogient.Cameleon.Forms.Sdk.Items;
using System.Web.UI;
using Alogient.Cameleon.Sdk;
using System.Resources;
using System.Reflection;
using System.Web;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    public class CMFormPage : PlaceHolder
    {
        public const string PageIDFormat = "page_{0}";
        public const string PageTitleIDFormat = "PageTitle_{0}";

        public CMFormPage(FormPage page)
        {
            this.ID = string.Format(PageIDFormat, page.ID.ToString());
            this.Visible = false;

            var pageTitle = new Literal { Text = string.Format("<h1>{0}</h1>", page.Name) };
            this.Controls.Add(pageTitle);

            // Generate Elements
            this.GenerateElements(page, this);

            // Generate Step buttons
            System.Web.UI.WebControls.Button backButton = new System.Web.UI.WebControls.Button();
            backButton.Text = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetLabelsRessource("Back");
            backButton.Click += new EventHandler(this.backButton_Click);
            System.Web.UI.WebControls.Button nextButton = new System.Web.UI.WebControls.Button();
            nextButton.Text = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetLabelsRessource("Next"); 
            nextButton.ValidationGroup = page.FormID.ToString();
            nextButton.Click += new EventHandler(this.nextButton_Click);

            // First Page with other pages 
            if (page.Form.Pages[0].ID == page.ID && page.Form.Pages.Count > 1)
            {
                this.Controls.Add(nextButton);
            }
            else if (page.Form.Pages[page.Form.Pages.Count - 1].ID == page.ID && page.Form.ShowSummary)
            {
                // Last Page and Show Summary
                if (page.Form.Pages.Count > 1)
                {
                    this.Controls.Add(backButton);
                }

                nextButton.Text = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetLabelsRessource("Summary"); 
                this.Controls.Add(nextButton);
            }
            else if (page.Form.Pages[page.Form.Pages.Count -1].ID == page.ID)
            {
                // Last Page and not Show Summary
                if (page.Form.Pages.Count > 1)
                {
                    this.Controls.Add(backButton);
                }

                nextButton.Text = Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetLabelsRessource("Finish");
                this.Controls.Add(nextButton);
            }
            else
            {
                this.Controls.Add(backButton);
                this.Controls.Add(nextButton);
            }
        }

        protected int GetPageStep()
        {
            int page = 0;
            TextBox formStatusBox = this.Parent.FindControl(Forms.Sdk.Constants.FormStatusBoxID) as TextBox;

            if (formStatusBox != null && !string.IsNullOrEmpty(formStatusBox.Text))
            {
                page = Int32.Parse(formStatusBox.Text);
            }

            return page;
        }

        protected void backButton_Click(object sender, EventArgs e)
        {
            this.ChangePageStep(this.GetPageStep() - 1);
        }
        protected void nextButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                this.ChangePageStep(this.GetPageStep() + 1);
            }
        }

        protected void ChangePageStep(int step)
        {
            var formStatusBox = this.Parent.FindControl(Forms.Sdk.Constants.FormStatusBoxID) as TextBox;

            if (formStatusBox != null)
            {
                formStatusBox.Text = step.ToString();
            }
        }

        #region Generate Elements

        protected void GenerateElements(FormPage page, PlaceHolder ph)
        {
            var div = new WebControl(HtmlTextWriterTag.Div)
                          {
                              ID = string.Format("PageDiv_{0}", page.ID.ToString())
                          };

            foreach (var element in page.Elements)
            {
                if ((CMServices.IsLive && element.VisibleLive) || (!CMServices.IsLive && element.VisiblePreview))
                {
                    switch (element.InputType)
                    {
                        case FormInputType.Checkbox: div.Controls.Add(new FormCheckbox(element as Forms.Sdk.Items.Checkbox)); break;
                        case FormInputType.Date: div.Controls.Add(new FormDate(element as Forms.Sdk.Items.Date)); break;
                        case FormInputType.DateRange: div.Controls.Add(new FormDateRange(element as Forms.Sdk.Items.DateRange)); break;
                        case FormInputType.Number: div.Controls.Add(new FormNumber(element as Forms.Sdk.Items.Number)); break;
                        case FormInputType.Radio: div.Controls.Add(new FormRadio(element as Forms.Sdk.Items.Radio)); break;
                        case FormInputType.Rating: div.Controls.Add(new FormRating(element as Forms.Sdk.Items.Rating)); break;
                        case FormInputType.Select: div.Controls.Add(new FormSelect(element as Forms.Sdk.Items.Select)); break;
                        case FormInputType.Text: div.Controls.Add(new FormText(element as Forms.Sdk.Items.Text)); break;
                        case FormInputType.TextArea: div.Controls.Add(new FormTextArea(element as Forms.Sdk.Items.TextArea)); break;
                        case FormInputType.TextBlock: div.Controls.Add(new FormTextBlock(element as Forms.Sdk.Items.TextBlock)); break;
                        case FormInputType.Upload: div.Controls.Add(new FormUpload(element as Forms.Sdk.Items.Upload)); break;
                        default: break;
                    }
                }
            }

            ph.Controls.Add(div);
        }

        #endregion
    }
}
